<?php
class WeixinWebOAuth{
	
	/**
	 * 跳转微信认证
	 * @param  string $callback_url [description]
	 * @return [type]               [description]
	 */
	public function goto_weixin($callback_url=''){
		$weixin_appid = C('weixin_appid');
		$url = 'https://open.weixin.qq.com/connect/qrconnect?appid='.$weixin_appid.'&redirect_uri='.$callback_url.'&response_type=code&scope=SCOPE&state=STATE#wechat_redirect';
		redirect($url);
	}
	/**
	 * 获取TOKEN和openid
	 * @param  [type] $code code码
	 * @return [type]       token
	 */
	public function get_token_openid($code){
		$info = '';
		$weixin_appid     = C('weixin_appid');
		$weixin_appsecret = C('weixin_appsecret');
		$url  = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$weixin_appid.'&secret='.$weixin_appsecret.'&code='.$code.'&grant_type=authorization_code';
		$data = file_get_contents($url);
		if($data){
			$json = json_decode($data,true);
			$info['token']  = $json['access_token'];
			$info['openid'] = $json['openid'];
		}
		return $info;
	}
	/**
	 * 获取用户信息
	 * @param  [type] $token  [description]
	 * @param  [type] $openid [description]
	 * @return [type]         [description]
	 */
	public function get_user_info($token,$openid){
		$url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$token.'&openid='.$openid;
		$data = file_get_contents($url);
		if($data){
			$json = json_decode($data,true);
		}
		return $json;
	}
}
?>